package com.mk.settlement.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.common.collect.Maps;
import com.mk.settlement.common.ExcelExport;
import com.mk.settlement.common.Page;
import com.mk.settlement.common.Util;
import com.mk.settlement.model.SettlementResult;
import com.mk.settlement.service.SettlementResultService;
import com.mk.settlement.service.SettlementService;

/**
 * Created by futao
 * description:
 */
@Controller
@RequestMapping("/sc/rpt/settlement")
public class SettlementResultController   {
	private Logger log = LoggerFactory.getLogger(SettlementResultController.class);

	@Autowired
	private SettlementResultService settlementResultService;
	
	@Autowired
	private SettlementService settlementService;
	
	   @RequestMapping(value = "/result",method = RequestMethod.POST)
	    @ResponseBody
	    public Map<String, Object> pageQuery(HttpServletRequest request, HttpServletResponse response, 
	    		SettlementResult sresult,Page page ){
//		    System.out.println(ToStringBuilder.reflectionToString(page));
//			System.out.println(ToStringBuilder.reflectionToString(sresult));
			log.info("查询参数是（page）："+ToStringBuilder.reflectionToString(page));
			log.info("查询参数是（SettlementResult）："+ToStringBuilder.reflectionToString(sresult));
	    	page= settlementResultService.findData(sresult, page);
	    	log.info("查询到"+page.getTotalRecord()+"条数据！");
	        Map<String, Object> rtnMap = Maps.newHashMap();
	        rtnMap.put("result", page.getResult());
 	        rtnMap.put("page", page);
	        return rtnMap;
	    }
	   
	   
		@RequestMapping(value = "/statements", method = RequestMethod.POST)
		@ResponseBody
		public Map<String, Object> statements( @RequestParam(value = "ids", required = true) @RequestBody String ids ) {
//			System.out.println("ids==="+ids);	
			log.info("未结算的数据结算，传入的ID是："+ids);
			Map<String, Object> rMap = Maps.newHashMap();
			int rs=settlementResultService.settlements(ids);
			log.info("未结算的数据结算，结算成功："+rs+"条数据！");
			rMap.put("result", rs);
			return rMap;
		}
		   
	 
	   
	   @RequestMapping(value = "/export", method = RequestMethod.POST)
		public void exportExcel(HttpServletRequest request,HttpServletResponse response,
				SettlementResult sresult,Page page ){
		    log.info("导出数据，传入的参数（page）："+ToStringBuilder.reflectionToString(page));
			log.info("导出数据，传入的参数（SettlementResult）："+ToStringBuilder.reflectionToString(sresult));
			String excelName = "结算数据导出";
			ExcelExport.exprotBefore(request,response,excelName);
			OutputStream fOut = null;
			try {
				String[] titile={"结算方名称","费用类型","结算总金额","折扣后总金额","状态","结算类型","收/支","销售类型","起始日期","结账日期","创建时间",};
				List<String[]> listData=settlementResultService.findExportData(titile.length, sresult,page);
				Boolean[] isdigit={null,null,true,true,null,null,null,null,null,null,null,null};
				int[] columnWidth={35,28,28,30,35,28,35,45,55,55,55 };
				HSSFWorkbook workbook =ExcelExport.export(excelName, titile,listData,columnWidth,isdigit);
				fOut = response.getOutputStream();
				workbook.write(fOut);
			} catch (UnsupportedEncodingException e1) {
				log.error("导出失败，失败原因："+e1.getMessage());
				e1.getMessage();
			} catch (Exception e) {
				log.error("导出失败，失败原因："+e.getMessage());
				e.getMessage();
			} finally {
				try {
					fOut.flush();
					fOut.close();
				} catch (IOException e) {
					e.getMessage();
				}
			}
			log.info("导出数据成功！");
//			System.out.println("文件生成...");
		}
	
	
	@RequestMapping(value = "/reSettlement", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> reSettlement(SettlementResult  sresult,
			@RequestParam(value = "sbt", required = false) @RequestBody Long sbt,
			@RequestParam(value = "set", required = false) @RequestBody Long set,
			@RequestParam(value = "ct", required = false) @RequestBody Long ct ) {
		sresult.setSettlementBeginTime(Util.date(sbt));
		sresult.setSettlementEndTime(Util.date(set));
		sresult.setCreateTime(Util.date(ct));
		log.info("重新结算的参数（SettlementResult）："+ToStringBuilder.reflectionToString(sresult));
//		System.out.println(ToStringBuilder.reflectionToString(sresult));
		Map<String, Object> rMap = Maps.newHashMap();
		boolean b=settlementService.reSettlement(sresult);
		log.info("重新结算的参数结果："+b);
		rMap.put("result", b);
		return rMap;
	}
	   
    
     
}